import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    redirect: 'admin'
  },
  {
    path: '/auth',
    name: 'Auth',
    component: () => import('../views/auth/index.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../layout/index.vue'),
    children: [
      {
        path: '',
        name: 'index',
        component: () => import('../views/admin/index/index.vue')
      },
      {
        path: 'system',
        name: 'System',
        component: () => import('../views/admin/system/index.vue')
      },
      {
        path: 'health',
        name: 'Health ',
        component: () => import('../views/admin/ambient/index.vue')
      },
      {
        path: 'password',
        name: 'password ',
        component: () => import('../views/admin/changePwd/index.vue')
      },
      {
        path: 'nightBelongs',
        name: 'nightBelongs ',
        component: () => import('../views/admin/outlate/index.vue')
      },
      {
        path: 'visitor',
        name: 'visitor ',
        component: () => import('../views/admin/visitor/index.vue')
      },
      {
        path: 'maintenance',
        name: 'maintenance ',
        component: () => import('../views/admin/repair/index.vue')
      },
      {
        path: 'building',
        name: 'Building',
        component: () => import('../views/admin/building/index.vue')
      },
      {
        path: 'administrator',
        name: 'Administrator',
        component: () => import('../views/admin/administrator/index.vue')
      },
      {
        path: '/:pathMatch(.*)', // /:pathMatch(.*)*的*把路径用/切割数组
        name: 'notFound',
        component: () => import('../views/notFount/404.vue')
      }

    ],
    beforeEnter: (to, from, next) => {
      const isLogin = localCache.getCache('token')
      if (isLogin) next()
      else next('/auth')
    }
  }
  // {
  //   path: '/404', // /:pathMatch(.*)*的*把路径用/切割数组
  //   name: 'notFound',
  //   component: () => import('../views/notFount/404.vue')
  // }
]

const router = createRouter({
  history: createWebHistory('/spring-boot-ui/'),
  routes
})

export default router
